package com.suge.send.processor;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.suge.common.result.message.MessageResult;
import com.suge.home.module.screen.Index;
import com.suge.message.dao.SmsMessageDAO;
import com.suge.message.dao.dataobject.SmsMessageDO;
import com.suge.order.dao.SmsOrderDAO;
import com.suge.order.dao.UserBizOrderDAO;
import com.suge.order.dao.dataobject.SmsOrderDO;
import com.suge.order.dao.dataobject.UserBizOrderDO;
import com.suge.send.http.SendParamDO;
import com.suge.send.http.SmsMessageSendClient;

public class SmsOrderProcessor  implements Runnable{

	private static final Logger logger = LoggerFactory.getLogger(SmsOrderProcessor.class);
	
	private static final int DAY_DELAY_TIME = 5000; 
	
	private static final String serverUrl = "http://www.baidu.com";
	
	private SmsOrderDAO smsOrderDAO;
	
	private UserBizOrderDAO userBizOrderDAO;

	private SmsMessageDAO smsMessageDAO;
	
	public void run() {

		try {
			
			Date date = new Date();
			if(date.getHours()>=21 || date.getHours()<9){
				//logger.error("It no send time! Wait 9am---9pm !");
				return;
			}
			
			 List<SmsOrderDO> orderList = smsOrderDAO.getSmsOrderNeed2Send();
			 logger.error("Need send order List!");
			 if(orderList==null || orderList.size()<1){
				// logger.error("Need send order List is null!");
				 return; 
			 }
			 SmsMessageSendClient smsMessageSendClient = new SmsMessageSendClient();
			 for(SmsOrderDO smsOrderDO : orderList){
				 SendParamDO sendParamDO =new SendParamDO();
				 String[] cMNumbers = {"134","135","136","137","138","139","147","150","151","152","157","158","159","182","183","187","188"}; 
				 boolean isNotSendNumber=false;
				 if(smsOrderDO.getBuyerTel()==null || smsOrderDO.getBuyerTel()=="" ||(smsOrderDO.getBuyerTel().length())!=11){
					 isNotSendNumber=true;
					 smsOrderDO.setStatus(-1);
					 smsOrderDAO.updateSmsOrderStatus(smsOrderDO);
					 continue;
				 }
				 String currentNum = smsOrderDO.getBuyerTel();
				 for(String str:cMNumbers){
					 if(currentNum.startsWith(str)){
						 isNotSendNumber=false;
					 }
				 }
				 if(isNotSendNumber){
					 smsOrderDO.setStatus(-1);
					 smsOrderDAO.updateSmsOrderStatus(smsOrderDO);
					 continue;
				 }
				 UserBizOrderDO userBizOrderDO = userBizOrderDAO.getUserBizOrderByUserNick(smsOrderDO.getUserNick());
				 if(userBizOrderDO==null || userBizOrderDO.getValidMessageCount()<=0){
					 smsOrderDO.setStatus(-2);
					 smsOrderDAO.updateSmsOrderStatus(smsOrderDO);
					 continue;
				 }
				
				 boolean messageOpen=false;
				 MessageResult messageResult = smsMessageDAO.getSmsMessageTempletesByUserNick(smsOrderDO.getUserNick());
				 List<SmsMessageDO> messageList = messageResult.getSuccessList();
				 if(messageList!=null){
						for(SmsMessageDO sms:messageList){
							if(sms.getTypeId()==smsOrderDO.getOrderType() && smsOrderDO.getStatus()==0){
								messageOpen=true;
							}
						}
				 }
				 if(!messageOpen){
					 smsOrderDO.setStatus(-3);
					 smsOrderDAO.updateSmsOrderStatus(smsOrderDO);
					 continue;
				 }
				 sendParamDO.getParamsMap().put("tel", smsOrderDO.getBuyerTel());
				 sendParamDO.getParamsMap().put("content",smsOrderDO.getOrderMessageContent());
				 sendParamDO.setPost(false);
				 sendParamDO.setServerUrl(serverUrl);
				 boolean sendResult = smsMessageSendClient.send(sendParamDO);
				 if(sendResult){
					 smsOrderDO.setStatus(1);
					 smsOrderDAO.updateSmsOrderStatus(smsOrderDO);
				 }
				 else{
					 logger.error("Sms message send error, smsOrderId:"+smsOrderDO.getOrderId());
				 }
			 }
			
		} catch (Exception e) {
			logger.error(e.toString());	
		}
		
	}

	public void setSmsOrderDAO(SmsOrderDAO smsOrderDAO) {
		this.smsOrderDAO = smsOrderDAO;
	}

	public SmsOrderDAO getSmsOrderDAO() {
		return smsOrderDAO;
	}

	public UserBizOrderDAO getUserBizOrderDAO() {
		return userBizOrderDAO;
	}

	public void setUserBizOrderDAO(UserBizOrderDAO userBizOrderDAO) {
		this.userBizOrderDAO = userBizOrderDAO;
	}

	public SmsMessageDAO getSmsMessageDAO() {
		return smsMessageDAO;
	}

	public void setSmsMessageDAO(SmsMessageDAO smsMessageDAO) {
		this.smsMessageDAO = smsMessageDAO;
	}

}
